package com.terios;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.terios.http.JsonAnalysis;
import com.weibo.net.WeiboException;

/**
 * Comment.java 2012-10-20 上午2:32:18
 * 
 * @author Wang Tengrui
 */
public class Comment extends JsonAnalysis implements Serializable {

	private static final long serialVersionUID = -84678341412294685L;
	private Date createdAt; // 评论时间
	private long id; // 评论id
	private String text; // 评论内容
	private String source; // 内容来源
	private User user; // 评论的作者
	private Status status; // 评论的微博
	private ReplyComment reply_comment;// 评论来源评论，当本评论属于对另一评论的回复时返回此字段

	public Comment(JSONObject commentJsonObject) throws WeiboException {
		super();
		init(commentJsonObject);
	}

	public static List<Comment> commentList(String responseString) throws WeiboException {
		JSONObject jsonObject = getJSONObject(responseString);
		JSONArray commentJsonArray;
		try {
			commentJsonArray = jsonObject.getJSONArray("comments");
			return commentList(commentJsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static List<Comment> commentList(JSONArray commentJsonArray) throws WeiboException {
		try {
			int size = commentJsonArray.length();
			List<Comment> comments = new ArrayList<Comment>(size);
			for (int i = 0; i < size; i++) {
				comments.add(new Comment(commentJsonArray.getJSONObject(i)));
			}
			return comments;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void init(JSONObject commentJsonObject) throws WeiboException {
		try {
			createdAt = parseDate(commentJsonObject.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
			id = getLong("id", commentJsonObject);
			text = commentJsonObject.getString("text");
			user = new User(commentJsonObject.getJSONObject("user"));
			status = new Status(commentJsonObject.getJSONObject("status"));
			if (!commentJsonObject.isNull("reply_comment")) {
				reply_comment = new ReplyComment(commentJsonObject.getJSONObject("reply_comment"));
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public ReplyComment getReply_comment() {
		return reply_comment;
	}

	public void setReply_comment(ReplyComment reply_comment) {
		this.reply_comment = reply_comment;
	}

}
